home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 011 / arc42s.lbr / ARCIO.MQC / arcio.mac
Text File  |  1985-08-04  |  4KB  |  101 lines

  1. /*  ARC - Archive utility - ARCIO
  2.  
  3. $define(tag,$$segment(@1,$$index(@1,=)+1))#
  4. $define(version,Version $tag(
  5. TED_VERSION DB =2.21), created on $tag(
  6. TED_DATE DB =06/25/85) at $tag(
  7. TED_TIME DB =11:08:24))#
  8. $undefine(tag)#
  9.     $version
  10.  
  11. (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
  12.  
  13.     By:  Thom Henderson
  14.  
  15.     Description:
  16.          This file contains the file I/O routines used to manipulate
  17.          an archive.
  18.  
  19.     Language:
  20.          Computer Innovations Optimizing C86
  21. */
  22. #include <stdio.h>
  23. #include "arc.h"
  24.  
  25. int readhdr(hdr,f)                     /* read a header from an archive */
  26. struct heads *hdr;                     /* storage for header */
  27. FILE *f;                               /* archive to read header from */
  28. {
  29.     char name[$fnlen];                 /* filename buffer */
  30.     int try = 10;                      /* retry counter */
  31.  
  32.     if(!f)                             /* if archive didn't open */
  33.          return 0;                     /* then pretend it's the end */
  34.     if(feof(f))                        /* if no more data */
  35.          return 0;                     /* then signal end of archive */
  36.  
  37.     while(fgetc(f)!=$arcmark)          /* ensure archive validity */
  38.     {    if(!try--)
  39.               abort("%s is not an archive",arcname);
  40.          if(warn)
  41.               printf("%s is not an archive, or is out of sync\n",arcname);
  42.          if(feof(f))
  43.               abort("Archive length error");
  44.     }
  45.  
  46.     hdrver = fgetc(f);                 /* get header version */
  47.     if(hdrver<0)
  48.          abort("Invalid header in archive %s",arcname);
  49.     if(hdrver==0)
  50.          return 0;                     /* note our end of archive marker */
  51.     if(hdrver>$arcver)
  52.     {    fread(name,sizeof(char),$fnlen,f);
  53.          printf("I don't know how to handle file %s in archive %s\n",
  54.               name,arcname);
  55.          printf("I think you need a newer version of ARC.\n");
  56.          exit(1);
  57.     }
  58.  
  59.     /* amount to read depends on header type */
  60.  
  61.     if(hdrver==1)                      /* old style is shorter */
  62.     {    fread(hdr,sizeof(struct heads)-sizeof(long int),1,f);
  63.          hdrver = 2;                   /* convert header to new format */
  64.          hdr->length = hdr->size;      /* size is same when not packed */
  65.     }
  66.     else fread(hdr,sizeof(struct heads),1,f);
  67.     return 1;                          /* we read something */
  68. }
  69.  
  70. writehdr(hdr,f)                        /* write a header to an archive */
  71. struct heads *hdr;                     /* header to write */
  72. FILE *f;                               /* archive to write to */
  73. {
  74.     fputc($arcmark,f);                 /* write out the mark of ARC */
  75.     fputc(hdrver,f);                   /* write out the header version */
  76.     if(!hdrver)                        /* if that's the end */
  77.          return;                       /* then write no more */
  78.     fwrite(hdr,sizeof(struct heads),1,f);
  79.  
  80.     /* note the newest file for updating the archive timestamp */
  81.  
  82.     if(hdr->date>arcdate
  83.     ||(hdr->date==arcdate && hdr->time>arctime))
  84.     {    arcdate = hdr->date;
  85.          arctime = hdr->time;
  86.     }
  87. }
  88.  
  89. filecopy(f,t,size)                     /* bulk file copier */
  90. FILE *f, *t;                           /* from, to */
  91. long size;                             /* number of bytes */
  92. $define(bufmax,4096)#                  /* buffer size to use */
  93. {
  94.     char buf[$bufmax];                 /* copy buffer */
  95.     int len;                           /* length of a given copy */
  96.  
  97.     while(size--)                      /* while more bytes to move */
  98.          if(fputc(fgetc(f),t)==EOF)    /* if error copying byte */
  99.               abort("Write fail (disk full?)");
  100. }
  101.